package day4;

import java.io.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * @author ryy
 * @year 2022年06月06日14:20
 */

public class Demo3 {
    public static void write() {
        Connection conn = null;
        PreparedStatement ps = null;

        try {
            //加载驱动
            Class.forName("com.mysql.jdbc.Driver");
            //定义连接字符串
            String url = "jdbc:mysql://localhost:3306/novels?characterEncoding=UTF-8";
            //用户名
            String user = "root";
            //密码
            String pwd = "root";
            //获取数据库连接
            conn = DriverManager.getConnection(url, user, pwd);
            //要执行的sql语句
            String sql = "insert into myNovel(id,novel) values(? , ?)";
            //创建PreparedStatement 处理text和 blob 必须使用创建PreparedStatement
            ps = conn.prepareStatement(sql);
            //设置id为1
            ps.setInt(1, 4);
            File file = new File("D:\\盗墓笔记.txt");
            //将小说转为字符输入流 并设置编码格式为 GBK
            Reader reader = new InputStreamReader(new FileInputStream(file), "GBK");
            //将字符输入流写入数据库
            ps.setCharacterStream(2, reader, (int) file.length());
            int result = ps.executeUpdate();
            if (result > 0) {
                System.out.println("盗墓笔记写入成功");
            } else {
                System.out.println("盗墓笔记写入失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //释放资源
            if (ps != null) {
                try {
                    ps.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void main(String[] args) {
        write();
    }
}
public class Demo03 {
 public static void read() {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            //加载驱动
            Class.forName("com.mysql.jdbc.Driver");
            //定义连接字符串
            String url = "jdbc:mysql://localhost:3306/novels?characterEncoding=UTF-8";
            //用户名
            String user = "root";
            //密码
            String pwd = "root";
            //获取数据库连接
            conn = DriverManager.getConnection(url, user, pwd);
            //要执行的sql语句
            String sql = "select * from myNovel where id = ?";
            //创建PreparedStatement 处理text和 blob 必须使用创建PreparedStatement
            ps = conn.prepareStatement(sql);
            //设置id为1
            ps.setInt(1, 1);
//            获取结果集
            if (rs.next()) {
                //读取
                Reader reader = rs.getCharacterStream("novel");
                //小说的输出目录为src(相对路径)
                Writer writer = new FileWriter("src/盗墓笔记.txt");
                char[] temp = new char[200];
                int len = 1;
                while ((len = reader.read(temp)) != -1) {
                    writer.write(temp, 0, len);
                }
                writer.close();
                reader.close();
                System.out.println("盗墓笔记读取成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            {
                if (rs != null) {
                    try {
                        rs.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
                if (ps != null) {
                    try {
                        ps.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
                if (conn != null) {
                    try {
                        conn.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
            }

        }

    }

    public static void main(String[] args) {
        read();
    }
}